layui.define(['layer', 'jquery'], function (exports) {
  var $ = layui.$,
      layer = layui.layer,
      ctoken = Util.nuid();

  var common = {
    checkMobile: function () {
      $.get('/copyright/realNameAuth/hasRealnameAuth', function (result) {
        if (result.success) {
          if (result.data == 1 || result.data == 2) {
            layer.open({
              area: ['520px', '380px'],
              content: $('#pop-bind-mobile'),
              type: 1,
              title: false,
              closeBtn: 0
            })
          }

        }
      }, 'json')
    },
    login: function (){
      var username = $('input[name="login_username"]').val(),
          password = $('input[name="login_password"]').val();
      if(username==''){
        layer.msg('请输入用户名');
        return false;
      }
      $.ajax({
        url: '/login',
        type: 'post',
        data: {username: username, password: password},
        success: function (result) {
          var username = result.username;

          username = username.substr(0, 4) + '...'
          var accountInfoHtml = '<span>' + username + '</span>'
          $('.right .user .user-hd').append(accountInfoHtml);
          $('.right .user').show();
          $('.right .un-login').hide();
          layer.closeAll();
        },
        error: function () {
          layer.msg('用户名或密码错误');
        }
      })
    },
    bindEvent: function () {

      $(document).keyup(function(event){
        if(event.keyCode ==13){
          if($('#pop_login_btn').is(":visible")){
            common.login();
          }
          if($('#login_btn').is(':visible')){
            $('#login_btn').click();
          }
        }
      });

      $('.un-login ul li:first a').click(function () {
        layer.open({
          area: ['520px', '470px'],
          content: $('#login_wrap'),
          type: 1,
          title: false,
          closeBtn: 1,
          success: function (index, layero) {
            $(':focus').blur();
          }
        })
      })

      $('.un-login ul li:last a').click(function () {
        layer.open({
          area: ['520px', '610px'],
          content: $('#reg_wrap'),
          type: 1,
          title: false,
          closeBtn: 1,
          success: function (index, layero) {
            $(':focus').blur();
          }
        })
      })

      //pop弹出框登录
      $('#pop_login_btn').click(function () {
        common.login();

      })

      $('.reg_btn').click(function () {
        layer.closeAll();
        $('.un-login ul li:last a').click();
      })

      $('#reg-btn').click(function () {
        var username = $("#extend_field5").val(),
            password = $("#password").val(),
            confirm_password = $("#confirm_password").val(),
            sms_code = $("#sms_code").val();
        if (Util.isEmpty(username)) {
          layer.msg('手机号不能为空');
        }
        if (Util.isEmpty(password)) {
          layer.msg('密码不能为空');
        }
        if (password != confirm_password) {
          layer.msg('两次输入密码不一致');
        }
        if (Util.isEmpty(sms_code)) {
          layer.msg('手机验证码不能为空');
        }
        $.post('/account/userRegister', {
          userName: username,
          password: password,
          confirm_password: confirm_password,
          verifyCode: sms_code,
          ctoken: ctoken
        }, function (data) {
          if (data.success) {
            layer.msg("注册成功");
            $.post('/login', {username: username, password: password},
                function (result) {
                  setTimeout(function () {
                    window.location.reload();
                  }, 1000)
                }, 'json')

          } else {
            layer.msg(data.message);
          }
        }, "json")
      })

      //验证码 注册
      $('#getcode').on('click', function (event) {
        if ($(this).hasClass('disabled')) {
          return false;
        }
        $(this).addClass('disabled');
        var mobile_phone = $("#extend_field5").val();
        if (mobile_phone.length < 11 || mobile_phone.length > 11) {
          layer.msg('手机号不能为空');
          return false;
        }

        var ths = $(this);
        var val = $(event.target)[0].tagName == 'INPUT' ? ths.val()
            : ths.html();
        var isinput = $(event.target)[0].tagName == 'INPUT' ? true : false;
        var time = 60;

        if (isinput) {
          ths.val(time + '秒后可重发');
        }
        else {
          ths.html(time + '秒后可重发');
        }

        _timeRun = setInterval(function () {
          if (time == 1) {
            ths.css('cursor', 'pointer');
            if (isinput) {

              ths.val(val);
            }
            else {

              ths.html(val);
            }
            clearInterval(_timeRun);
            ths.removeClass('disabled');
          }
          if (time > 1) {
            time--;
            //console.log(time);
            ths.css('cursor', 'default');

            if (isinput) {
              ths.val(time + '秒后可重发');
            }
            else {
              ths.html(time + '秒后可重发');
            }
          }

        }, 1000);

        var index = layer.load(1, {shade: [0.1, '#fff']});
        $.getJSON("/account/sendValidateSms",
            {
              mobile: $("#extend_field5").val(),
              ctoken: ctoken,
            },
            function (data) {
              layer.close(index);
              if (data.success) {
                layer.msg('短信验证码已发送');
              } else {
                layer.msg(data.message);
                time = 1;
              }
            });

        return false;
      })

      $('#verify_code_btn').click(function () {
        if ($(this).hasClass('disabled')) {
          return false;
        }
        var mobile = $('input[name="mobile"]').val();
        if (!mobile) {
          layer.msg('请输入手机号码');
          return false;
        }
        var mobileReg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if (!mobileReg.test(mobile)) {
          layer.msg('请输入正确的手机号码');
          return false;
        }

        var ths = $(this);
        var val = $(event.target)[0].tagName == 'INPUT' ? ths.val()
            : ths.html();
        var isinput = $(event.target)[0].tagName == 'INPUT' ? true : false;
        var time = 60;

        if (isinput) {
          ths.val(time + '秒后可重发');
        }
        else {
          ths.html(time + '秒后可重发');
        }

        _timeRun = setInterval(function () {
          if (time == 1) {
            ths.css('cursor', 'pointer');
            if (isinput) {

              ths.val(val);
            }
            else {

              ths.html(val);
            }
            clearInterval(_timeRun);
            ths.removeClass('disabled');
          }
          if (time > 1) {
            time--;
            //console.log(time);
            ths.css('cursor', 'default');

            if (isinput) {
              ths.val(time + '秒后可重发');
            }
            else {
              ths.html(time + '秒后可重发');
            }
          }

        }, 1000);

        $.get('/copyright/realNameAuth/sendBindMobileSms', {mobile: mobile},
            function (result) {
              if (!result.success) {
                layer.msg(result.message);
              } else {
                $(this).addClass('disabled');

              }
            })
      })

      $('#sub-btn').click(function () {
        var mobile = $('input[name="mobile"]').val(),
            verifyCode = $('input[name="sms_code_bind_mobile"]').val();
        if (!mobile) {
          layer.msg('请输入手机号码');
          return false;
        }
        if (!verifyCode) {
          layer.msg('请输入验证码');
          return false;
        }
        $.post('/copyright/realNameAuth/bindMobile',
            {mobile: mobile, verifyCode: verifyCode}, function (result) {
              if (result.success) {
                var data = result.data;
                if (data == 1) {
                  layer.closeAll();
                } else if (data == 2) {
                  location.href = '/copyright/realname/authentication';
                }
              } else {
                layer.msg(result.message);
              }
            }, 'json')
      })

    },
    initBaiduTj: function () {
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c28e68868ecfc89701c591a250f6a1f1";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    }
  }
  common.checkMobile();
  common.bindEvent();
  common.initBaiduTj();
  exports('common', {});
})